import type { GlobConfig } from '/#/config';

import { warn } from '/@/utils/log';
import { getAppEnvConfig } from '/@/utils/env/env';

export const useGlobSetting = (): Readonly<GlobConfig> => {
  const {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
    VITE_GLOB_APP_EN_NAME,
    VITE_PORT,
  } = getAppEnvConfig();

  const CONVERTED_TITLE = VITE_GLOB_APP_TITLE.replace(/ /g, ' ');
  /* eslint-disable */
  if (!/[a-zA-Z\_]*/.test(VITE_GLOB_APP_SHORT_NAME)) {
    warn(
      `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`
    );
  }
  /* eslint-disable */
  // Take global configuration
  const glob: Readonly<GlobConfig> = {
    title: CONVERTED_TITLE,
    apiUrl: VITE_GLOB_API_URL,
    shortName: VITE_GLOB_APP_SHORT_NAME,
    urlPrefix: VITE_GLOB_API_URL_PREFIX,
    uploadUrl: VITE_GLOB_UPLOAD_URL,
    appEnName: VITE_GLOB_APP_EN_NAME,
    appDomain: import.meta.env.VITE_PUBLIC_PATH + VITE_GLOB_APP_EN_NAME + '/',
    devPort: VITE_PORT,
  };
  return glob as Readonly<GlobConfig>;
};
